package models

import (
	"360video/video-srv/handler/config"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Account  string `json:"account"`
	Password string `json:"password"`
}

func Register[T any](data *T) error {
	return config.DB.Create(&data).Error
}
func Login[T any](data *T) error {
	return config.DB.Find(&data).Error
}

type Video struct {
	gorm.Model
	UserId  int    `json:"user_id"`
	Titer   string `json:"titer"`
	Message string `json:"message"`
	Static  string `json:"static"`
	Address string `json:"address"`
	Year    string `json:"year"`
	Image   string `json:"image"`
}

func VideoAdd[T any](data *T) error {
	return config.DB.Create(&data).Error
}
func VideoList[T any](data *[]T, size, page int, address string) error {
	switch {
	case size > 100:
		size = 100
	case size <= 0:
		size = 10
	}

	offset := (page - 1) * size
	return config.DB.Offset(offset).Limit(size).Where("address=?", address).Find(&data).Error
}
func VideoDetails[T any](data *T, id int) error {
	return config.DB.Where("id=?", id).Find(&data).Error
}

func VideoType[T any](data *T, static string) error {
	return config.DB.Where("static=?", static).Find(&data).Error
}

type Episode struct {
	gorm.Model
	VideoId int    `json:"video_id"`
	Title   string `json:"title"`
	Time    string `json:"time"`
}

func EpisodeList[T any](data *[]T) error {
	return config.DB.Find(&data).Error
}

type HisTory struct {
	gorm.Model
	UserId    int `json:"user_id"`
	VideoId   int `json:"video_id"`
	EpisodeId int `json:"episode_id"`
	Progress  int `json:"progress"`
}

func HisToryAdd[T any](data *T) error {
	return config.DB.Create(&data).Error
}
func HisToryList[T any](data *T) error {
	return config.DB.Order("created_at desc").Find(&data).Error
}

type Hot struct {
	gorm.Model
	VideoId   int     `json:"video_id"`
	ViewCount float64 `json:"view_count"`
	LikeCount float64 `json:"like_count"`
	CollCount float64 `json:"coll_count"`
	TakeCount float64 `json:"take_count"`
	HotNum    float64 `json:"hot_num"`
}

func HotList[T any](data *T) error {
	return config.DB.Find(&data).Error
}
